package org.leaf.green.b2b

import org.springframework.transaction.annotation.*
import org.hibernate.FetchMode as FM

class MarketResearchAjaxService {

    static transactional = true

    //市场列表
    @Transactional(readOnly = true)
    def marketInstanceList(term) {
        def marketInstanceList = Market.withCriteria{

            createAlias 'region', 'reg'
            createAlias 'marketType', 'type'

            projections{
                property('id')
                property('serialNumber')
                property('name')
                property('reg.id')
                property('reg.serialNumber')
                property('reg.name')
                property('type.serialNumber')
                property('type.name')
            }

            fetchMode "region", FM.JOIN
            fetchMode "marketType", FM.JOIN

            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)

            order("serialNumber", "asc")
        }

        return marketInstanceList
    }

    //区域列表
    @Transactional(readOnly = true)
    def regionInstanceList(term){
        
        def regionInstanceList = Region.withCriteria{
        
            projections{
                property('id')
                property('serialNumber')
                property('name')
            }

            or{
                ilike("serialNumber", term + "%")
                ilike("name", "%"+term + "%")
            }
            maxResults(10)

            order("serialNumber", "asc")

        }

        return regionInstanceList
        
    }

    //门店列表
    @Transactional(readOnly=true)
    def retailerInstanceList(term){
        
        def result = Retailer.withCriteria{

            createAlias 'region', 'reg'
        
            projections{
            
                property('id')
                property('serialNumber')
                property('name')
                property('reg.id')
                property('reg.serialNumber')
                property('reg.name')
 
            }

            fetchMode "region", FM.JOIN

            or{
                ilike("serialNumber", "${term}%")
                ilike("name", "%${term}%")
            }

            maxResults(10)

            order("serialNumber", "asc")

        }

        return result
    }

    //取最近的采购价格
    @Transactional(readOnly = true)
    def lastPurchasePrice(product) {

        def result = PurchaseDetail.withCriteria(uniqueResult:true){

            createAlias "product", "prod"
            createAlias "purchase", "pur"
            createAlias "purchase.recordType", "type"
            
            projections{
                //property('pur.date')
                property('price')
            }

            fetchMode "product", FM.JOIN
            fetchMode "purchase", FM.JOIN
            fetchMode "purchase.recordType", FM.JOIN

            eq('prod.name', product)
            eq('type.serialNumber', '1')
            //eq('pur.approved', true)
            //le('pur.date', date)

            maxResults(1)

            order("pur.date", "desc")

        }

        return result
    }

    //今天的调研价格
    @Transactional(readOnly = true)
    def todayResearchPrice(product) {

        def result = MarketPriceResearchDetail.withCriteria(){

            //createAlias "product", "prod"
            createAlias "marketPriceResearch", "research"
            createAlias "marketPriceResearch.region", "region"
            createAlias "marketPriceResearch.market", "market"
            
            projections{
                property('region.name')
                property('market.name')
                property('price')
                property('promotion')
            }

            //fetchMode "product", FM.JOIN
            fetchMode "marketPriceResearch", FM.JOIN
            fetchMode "marketPriceResearch.region", FM.JOIN
            fetchMode "marketPriceResearch.market", FM.JOIN

            eq('product', product)
            eq('research.approved', true)
            //eq('research.date', new Date())

            //maxResults(1)
        }

        return result
    }


    
}
